package com.learn.seven;

import org.apache.log4j.Logger;

import java.util.concurrent.TimeUnit;

/**
 * @author ln
 * @Description: ${todo}
 * @date 2020/12/15
 */
public class testDeadLock {
    private static final Logger log = Logger.getLogger(testDeadLock.class);

    public static void main(String[] args) {
        test1();
    }

    private static void test1() {
        Object A = new Object();
        Object B = new Object();

        Thread t1 = new Thread(() -> {
            try {
                synchronized (A) {
                    log.debug("lock A");
                    TimeUnit.SECONDS.sleep(1);
                    synchronized (B) {
                        log.debug("lock B");
                        log.debug("操作....");
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t1");

        Thread t2 = new Thread(() -> {
            try {
                synchronized (B) {
                    log.debug("lock B");
                    Thread.sleep(500);
                    synchronized (A) {
                        log.debug("lock A");
                        log.debug("操作...");
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t2");

        t1.start();
        t2.start();


    }

}


